Chat with Postgresql Database
工作流概述
这是一个包含11个节点的复杂工作流,主要用于自动化处理各种任务。
工作流源代码
{
"id": "eOUewYsEzJmQixI6",
"meta": {
"instanceId": "77c4feba8f41570ef06dc76ece9a6ded0f0d44f7f1477a64c2d71a8508c11faa",
"templateCredsSetupCompleted": true
},
"name": "Chat with Postgresql Database",
"tags": [],
"nodes": [
{
"id": "6501a54f-a68c-452d-b353-d7e871ca3780",
"name": "When chat message received",
"type": "@n8n/n8n-nodes-langchain.chatTrigger",
"position": [
-300,
-80
],
"webhookId": "cf1de04f-3e38-426c-89f0-3bdb110a5dcf",
"parameters": {
"options": {}
},
"typeVersion": 1.1
},
{
"id": "cd32221b-2a36-408d-b57e-8115fcd810c9",
"name": "AI Agent",
"type": "@n8n/n8n-nodes-langchain.agent",
"position": [
0,
-80
],
"parameters": {
"agent": "openAiFunctionsAgent",
"options": {
"systemMessage": "You are DB assistant. You need to run queries in DB aligned with user requests.
Run custom SQL query to aggregate data and response to user. Make sure every table has schema prefix to it in sql query which you can get from `Get DB Schema and Tables List` tool.
Fetch all data to analyse it for response if needed.
## Tools
- Execute SQL query - Executes any sql query generated by AI
- Get DB Schema and Tables List - Lists all the tables in database with its schema name
- Get Table Definition - Gets the table definition from db using table name and schema name"
}
},
"typeVersion": 1.7
},
{
"id": "8accbeeb-7eaf-4e9e-aabc-de8ab3a0459b",
"name": "OpenAI Chat Model",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"position": [
-60,
160
],
"parameters": {
"model": {
"__rl": true,
"mode": "list",
"value": "gpt-4o-mini"
},
"options": {}
},
"credentials": {
"openAiApi": {
"id": "48uG61Ilo8jndw3r",
"name": "Your OpenAI Account Credentials"
}
},
"typeVersion": 1.2
},
{
"id": "11f2013f-a080-4c9e-8773-c90492e2c628",
"name": "Get Table Definition",
"type": "n8n-nodes-base.postgresTool",
"position": [
780,
140
],
"parameters": {
"query": "select
c.column_name,
c.data_type,
c.is_nullable,
c.column_default,
tc.constraint_type,
ccu.table_name AS referenced_table,
ccu.column_name AS referenced_column
from
information_schema.columns c
LEFT join
information_schema.key_column_usage kcu
ON c.table_name = kcu.table_name
AND c.column_name = kcu.column_name
LEFT join
information_schema.table_constraints tc
ON kcu.constraint_name = tc.constraint_name
AND tc.constraint_type = 'FOREIGN KEY'
LEFT join
information_schema.constraint_column_usage ccu
ON tc.constraint_name = ccu.constraint_name
where
c.table_name = '{{ $fromAI(\"table_name\") }}'
AND c.table_schema = '{{ $fromAI(\"schema_name\") }}'
order by
c.ordinal_position",
"options": {},
"operation": "executeQuery",
"descriptionType": "manual",
"toolDescription": "Get table definition to find all columns and types"
},
"credentials": {
"postgres": {
"id": "nGI61D0TEEZz18rr",
"name": "Your Postgresql Database Credentials"
}
},
"typeVersion": 2.5
},
{
"id": "760bc9bc-0057-4088-b3f0-3ee37b3519df",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"position": [
-300,
-240
],
"parameters": {
"color": 5,
"width": 560,
"height": 120,
"content": "### 👨🎤 Setup
1. Add your **postgresql** and **OpenAI** credentials.
2. Click **Chat** button and start asking questions to your database.
3. Activate the workflow and you can make the chat publicly available."
},
"typeVersion": 1
},
{
"id": "0df33341-c859-4a54-b6d9-a99670e8d76d",
"name": "Chat History",
"type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
"position": [
120,
160
],
"parameters": {},
"typeVersion": 1.3
},
{
"id": "4938b22e-f187-4ca0-b9f1-60835e823799",
"name": "Sticky Note3",
"type": "n8n-nodes-base.stickyNote",
"position": [
360,
300
],
"parameters": {
"color": 7,
"width": 562,
"height": 156,
"content": "🛠️ Tools Used:
1. Execute SQL Query: Used to execute any query generated by the agent.
2. Get DB Schema and Tables List: It returns the list of all the tables with its schema name.
3. Get Table Definition: It returns table details like column names, foreign keys and more of a particular table in a schema."
},
"typeVersion": 1
},
{
"id": "39780c78-4fbc-403e-a220-aa6a4b06df8c",
"name": "Sticky Note1",
"type": "n8n-nodes-base.stickyNote",
"position": [
-100,
300
],
"parameters": {
"color": 7,
"width": 162,
"height": 99,
"content": "👆 You can exchange this with any other chat model of your choice."
},
"typeVersion": 1
},
{
"id": "28a5692c-5003-46cb-9a09-b7867734f446",
"name": "Sticky Note2",
"type": "n8n-nodes-base.stickyNote",
"position": [
100,
300
],
"parameters": {
"color": 7,
"width": 162,
"height": 159,
"content": "👆 You can change how many number of messages to keep using `Context Window Length` option. It's 5 by default."
},
"typeVersion": 1
},
{
"id": "c18ced71-6330-4ba0-9c52-1bb5852b3039",
"name": "Execute SQL Query",
"type": "n8n-nodes-base.postgresTool",
"position": [
380,
140
],
"parameters": {
"query": "{{ $fromAI(\"sql_query\", \"SQL Query\") }}",
"options": {},
"operation": "executeQuery",
"descriptionType": "manual",
"toolDescription": "Get all the data from Postgres, make sure you append the tables with correct schema. Every table is associated with some schema in the database."
},
"credentials": {
"postgres": {
"id": "nGI61D0TEEZz18rr",
"name": "Your Postgresql Database Credentials"
}
},
"typeVersion": 2.5
},
{
"id": "557623c6-e499-48a6-a066-744f64f8b6f3",
"name": "Get DB Schema and Tables List",
"type": "n8n-nodes-base.postgresTool",
"position": [
580,
140
],
"parameters": {
"query": "SELECT
table_schema,
table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY table_schema, table_name;",
"options": {},
"operation": "executeQuery",
"descriptionType": "manual",
"toolDescription": "Get list of all tables with their schema in the database"
},
"credentials": {
"postgres": {
"id": "nGI61D0TEEZz18rr",
"name": "Your Postgresql Database Credentials"
}
},
"typeVersion": 2.5
}
],
"active": false,
"pinData": {},
"settings": {
"executionOrder": "v1"
},
"versionId": "10c7c74e-b383-4ac7-8cb2-c9a15a2818fe",
"connections": {
"Chat History": {
"ai_memory": [
[
{
"node": "AI Agent",
"type": "ai_memory",
"index": 0
}
]
]
},
"Execute SQL Query": {
"ai_tool": [
[
{
"node": "AI Agent",
"type": "ai_tool",
"index": 0
}
]
]
},
"OpenAI Chat Model": {
"ai_languageModel": [
[
{
"node": "AI Agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Get Table Definition": {
"ai_tool": [
[
{
"node": "AI Agent",
"type": "ai_tool",
"index": 0
}
]
]
},
"When chat message received": {
"main": [
[
{
"node": "AI Agent",
"type": "main",
"index": 0
}
]
]
},
"Get DB Schema and Tables List": {
"ai_tool": [
[
{
"node": "AI Agent",
"type": "ai_tool",
"index": 0
}
]
]
}
}
}
功能特点
- 自动检测新邮件
- AI智能内容分析
- 自定义分类规则
- 批量处理能力
- 详细的处理日志
技术分析
节点类型及作用
- @N8N/N8N Nodes Langchain.Chattrigger
- @N8N/N8N Nodes Langchain.Agent
- @N8N/N8N Nodes Langchain.Lmchatopenai
- Postgrestool
- Stickynote
复杂度评估
配置难度:
维护难度:
扩展性:
实施指南
前置条件
- 有效的Gmail账户
- n8n平台访问权限
- Google API凭证
- AI分类服务订阅
配置步骤
- 在n8n中导入工作流JSON文件
- 配置Gmail节点的认证信息
- 设置AI分类器的API密钥
- 自定义分类规则和标签映射
- 测试工作流执行
- 配置定时触发器(可选)
关键参数
| 参数名称 | 默认值 | 说明 |
|---|---|---|
| maxEmails | 50 | 单次处理的最大邮件数量 |
| confidenceThreshold | 0.8 | 分类置信度阈值 |
| autoLabel | true | 是否自动添加标签 |
最佳实践
优化建议
- 定期更新AI分类模型以提高准确性
- 根据邮件量调整处理批次大小
- 设置合理的分类置信度阈值
- 定期清理过期的分类规则
安全注意事项
- 妥善保管API密钥和认证信息
- 限制工作流的访问权限
- 定期审查处理日志
- 启用双因素认证保护Gmail账户
性能优化
- 使用增量处理减少重复工作
- 缓存频繁访问的数据
- 并行处理多个邮件分类任务
- 监控系统资源使用情况
故障排除
常见问题
邮件未被正确分类
检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。
Gmail认证失败
确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。
调试技巧
- 启用详细日志记录查看每个步骤的执行情况
- 使用测试邮件验证分类逻辑
- 检查网络连接和API服务状态
- 逐步执行工作流定位问题节点
错误处理
工作流包含以下错误处理机制:
- 网络超时自动重试(最多3次)
- API错误记录和告警
- 处理失败邮件的隔离机制
- 异常情况下的回滚操作